//index.js
//获取应用实例
const app = getApp()

import {
  banner,
  getCate,
  getCateII,
  lookAttack,
  getAttackK,
  discussList
} from '../../utils/api.js'

Page({
  data: {
    tt: 0,
    aa: 0,
    fontSize: 30,
    bFontSize: 40,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [],
    indicatorDots: false,
    childrenList: [],
    getCateList: [],
    arrayList: [],
    autoplay: true,
    interval: 3000,
    duration: 1000,
    current: 0,
    index: 0,
    currentSwiper: 0,
    currentindex: 0,
    idcurrent: '',
    count: "",
    bannerData: {
      level: "2",
    },
    gonglv: {
      id: "",
      attack_id: "",
      p: "",
    },
    getCateIIData: {
      p: 1,
      searchKey: "",
      shop_type_one: "",
      shop_type_two: "",
    },
    catToken:{
    },
    notimg: false,
    currentPage: 1,
    scrollHeight: 0,
    isdata: true
  },
  onShow:function() {
    var that=this
    // 获取屏幕高度
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight: res.windowHeight * 2
        })
      },
    })
    this.showMessage()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    if (this.data.isdata){
      wx.showLoading({
        title: '正在刷新数据...',
      });
      this.getList()
      that.get_Banner();
    }
  },
  //上拉
  onReachBottom: function () {
    if (this.data.isdata){
       this.data.getCateIIData.p++
      this.get_Cate();
      this.getList(true);
    }else{
      wx.stopPullDownRefresh() 
      wx.showToast({
        title: '暂时没有数据啦....',
        icon:"none"
      })
    }
  },
  search(e) {
    let searchKey = "getCateIIData.searchKey";
    this.setData({
      [searchKey]: e.detail.value
    })
    this.getList()
  },
   showMessage(){
     var that=this
     that.get_Banner()
     that.get_Cate()
     that.getList();
   },
  get_Banner() {
    var that=this
    //banner图
    banner(that.data.bannerData).then(res=>{
      that.setData({
        imgUrls: res.datas
      })
    })
  },
  get_Cate() {
    var that = this
    //联动一级
    getCate(this.data.catToken).then(res => {
      let alldata = [{
        id: 0,
        img: "../public/img/foot_icon_gl.png",
        name: "全部"
      }]
      let data = alldata.concat(res.datas)
      //var erji = res.datas.children;
      that.setData({
        getCateList: data
        // childrenList: res.datas.children
      })
    })
  },
  //封装获取数据
  getList(save = false) {
    wx.stopPullDownRefresh();
    //wx.showLoading({ title: '加载中' });
    if(!save) {
      this.data.getCateIIData.p = 1;
    }
    getAttackK(this.data.getCateIIData).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        this.setData({
          arrayList: save ? this.data.arrayList.concat(res.datas.list) : res.datas.list,
          isdata: res.datas.more
        })
      }else{
        this.setData({
          arrayList: []
        })
      }
    })
  },
  activityBtn(e) {
    let id = e.currentTarget.dataset.indexx;
    let getCateIIDataP = "getCateIIData.p";
    let ids = e.currentTarget.dataset.id;
    this.setData({
      aa: id
    })
    let childdata = this.data.arrayList;
    if (childdata) {
      this.setData({
        arrayList: childdata,
        [getCateIIDataP]: 1
      })
    } else {
      this.setData({
        arrayList: "",
        [getCateIIDataP]: 1
      })
    }
  },
  seachCate(e) {
    let id = e.currentTarget.dataset.indexs; //索引ID
    let ids = e.currentTarget.dataset.ids; //主键ID
    let getCateIIDataP = "getCateIIData.p";
    this.setData({
      tt: id
    })
    let shop_type_two_ = "getCateIIData.shop_type_two";
    this.setData({
      [shop_type_two_]: "",
      [getCateIIDataP]: 1
    })
    let shop_type_one_ = "getCateIIData.shop_type_one";
    this.setData({
      [shop_type_one_]: ids,
      [getCateIIDataP]: 1
    })
    let childdata = this.data.getCateList[id].children;
    if (childdata) {
      this.setData({
        childrenList: childdata,
        [getCateIIDataP]: 1
      })
      let shop_type_two_ = "getCateIIData.shop_type_two";
      this.setData({
        [shop_type_two_]: childdata[0].id
      })
    } else {
      this.setData({
        childrenList: "",
        [getCateIIDataP]: 1
      })
    }
  },
  autoSlide(e) {
    this.setData({
      arrayList: []
    })
    let current = e.detail.current;
    let id = this.data.getCateList[current].id;
    let childdata = this.data.getCateList[current].children;
    let getCateIIDataP = "getCateIIData.p"
    if (childdata) {
      this.setData({
        count: childdata.length,
      })
    }
    this.setData({
      tt: current,
    })
    let shop_type_two_ = "getCateIIData.shop_type_two";
    let shop_type_one_ = "getCateIIData.shop_type_one";
    this.setData({
      [shop_type_two_]: "",
      [shop_type_one_]: id,
      [getCateIIDataP]: 1
    })
    if (childdata) {
      this.setData({
        childrenList: childdata,
        [getCateIIDataP]: 1
      })
      let shop_type_two_ = "getCateIIData.shop_type_two";
      this.setData({
        [shop_type_two_]: childdata[0].id,
        [getCateIIDataP]: 1
      })
    } else {
      this.setData({
        childrenList: "",
        [getCateIIDataP]: 1
      })
    }
    this.getList();
  },
  businessDetails(e) {
    wx.navigateTo({
      url: './businessDetails/businessDetails',
    })
  },
  swiperChange(e) {
    this.setData({
      currentSwiper: e.detail.current
    })

  },
  /*景区详情查看*/
  details(e) {
   // let ids = e.currentTarget.dataset.id.id
  //  let attack_ids = e.currentTarget.dataset.id.attack_id
    let index = e.currentTarget.dataset.index
    let data = JSON.stringify(this.data.arrayList[index])
      wx.navigateTo({
        url: './strategyDetails/strategyDetails?data=' + data,
      })
  },
})